<template>
  <page>
    <template v-slot:header>
      <h2>
        <span class="icon iconfont">&#xe607;</span>
        <span>{{ $t("configure.configure_group") }}</span>
      </h2>
    </template>
    <template v-slot:content>
      <el-dialog
        :title="title"
        :visible.sync="display"
        :before-close="handleClose"
        width="60%"
      >
        <create-group
          v-if="type === 'create'"
          @click="handleClose"
        ></create-group>
        <delete
          v-else-if="type === 'delete'"
          :content="delete_content"
          :handleCancel="handleClose"
          :handleOk="handleDeleteOk"
        >
        </delete>
        <update-group
          v-else-if="type === 'update'"
          @click="handleClose"
          :row_id="row_id"
        >
        </update-group>
        <issue-form
          v-else-if="type === 'issue'"
          @click="handleClose"
          :standarName="standarName"
        ></issue-form>
        <system-table
          v-else-if="type === 'system'"
          :id="id"
          @click="handleClose"
        ></system-table>
        <configure-table
          v-else-if="type === 'report'"
          :id="id"
          :row_id="row_id"
          :batch_id="batch_id"
          @click="handleClose"
        ></configure-table>
        <import-configure
          v-else-if="type === 'import'"
          @click="handleClose"
        ></import-configure>
      </el-dialog>
      <p class="kylin-page-label">
        {{ $t("configure.configure_group_table_description") }}
      </p>
      <ky-table
        :getData="getConfigureGroup"
        ref="table"
        :showSelect="true"
        :search_data="search_data"
      >
        <template v-slot:table-action>
          <authorization-button class="kylin-item-button" @click="handleAdd">
            {{ $t("add") }}
          </authorization-button>
          <authorization-button
            class="kylin-item-button"
            @click="handleDelete"
            :disabled="$refs.table && $refs.table.selectRow.ids.length == 0"
          >
            {{ $t("delete") }}
          </authorization-button>
          <authorization-button class="kylin-item-button" @click="handleImport">
            {{ $t("import") }}
          </authorization-button>
          <authorization-button
            class="kylin-item-button"
            @click="handleExport"
            :disabled="$refs.table && $refs.table.selectRow.ids.length == 0"
          >
            {{ $t("export") }}
          </authorization-button>
        </template>
        <template v-slot:table>
          <el-table-column
            prop="name"
            :label="$t('configure.configure_group_name')"
          >
          </el-table-column>
          <el-table-column
            prop="standardLibraryName"
            :label="$t('configure.normalLibrary')"
          >
          </el-table-column>
          <el-table-column :label="$t('configure.configure_item')">
            <template slot-scope="scope">
              <router-link :to="$route.path + scope.row.id">
                {{ scope.row.configs }}
              </router-link>
            </template>
          </el-table-column>
          <el-table-column
            prop="groupAttribute"
            :label="$t('configure.ascription')"
          >
          </el-table-column>
          <el-table-column prop="founder" :label="$t('configure.creator')">
          </el-table-column>
          <el-table-column prop="revised" :label="$t('configure.reviser')">
          </el-table-column>
          <el-table-column
            prop="updatedAt"
            :label="$t('configure.last_revision_time')"
          >
          </el-table-column>
          <el-table-column
            prop="description"
            :label="$t('configure.description')"
          >
          </el-table-column>
          <el-table-column :label="$t('action')">
            <template slot-scope="scope">
              <authorization-button
                @click.native.prevent="handleEdit(scope.row)"
                type="text"
                size="medium"
                name="config:update"
              >
                {{ $t("edit") }}
              </authorization-button>
              <authorization-button
                @click.native.prevent="handleIssue(scope.row)"
                type="text"
                size="medium"
                name="config:update"
              >
                {{ $t("configure.issue") }}
              </authorization-button>
            </template>
          </el-table-column>
        </template>
      </ky-table>
    </template>
  </page>
</template>

<script>
import Page from "@/components/Page";
import kyTable from "@/components/KyTable";
import { getStandardLib } from "@/api/config";
import {
  getConfigureGroup,
  deleteConfigureGroup,
  downloadConfigureGroup,
} from "@/api/configure_group";
import AuthorizationButton from "@/components/AuthorizationButton";
import CreateGroup from "./form/CretaeGroup.vue";
import UpdateGroup from "./form/UpdateGroup.vue";
import IssueForm from "./form/IssueForm.vue";
import SystemTable from "./form/SystemTable.vue";
import Delete from "@/components/Delete";
import ConfigureTable from "./form/ConfigureTable.vue";
import ImportConfigure from "./form/importConfigure.vue";
import { download } from "@/utils";
export default {
  name: "configure_group_index",
  components: {
    Page,
    kyTable,
    AuthorizationButton,
    CreateGroup,
    UpdateGroup,
    IssueForm,
    Delete,
    SystemTable,
    ImportConfigure,
    ConfigureTable,
  },
  data() {
    return {
      search_data: {
        name: {
          name: "name",
          type: "text",
        },
        standardLibrary: {
          name: "configure.normalLibrary",
          type: "select",
          items: [],
          func: this.getNameVersion,
        },
        groupAttribute: {
          name: "configure.ascription",
          type: "select",
          items: [
            { key: "中心", name: "中心" },
            { key: "基地", name: "基地" },
            { key: "部门", name: "部门" },
          ],
        },
        founder: {
          name: "configure.creator",
          type: "text",
        },
        revised: {
          name: "configure.reviser",
          type: "text",
        },
        updatedAt: {
          name: "configure.last_revision_time",
          type: "date",
        },
      },
      title: "",
      fileList: [],
      display: false,
      type: "",
      delete_content: "",
      row_id: "",
      id: "",
      batch_id: "",
      standarName: "",
    };
  },
  activated() {
    this.$refs.table.handleSearch();
  },
  methods: {
    getConfigureGroup,
    getNameVersion() {
      getStandardLib({ paged: false }).then((res) => {
        res.data.data.results.map((item) => {
          this.search_data.standardLibrary.items.push({
            key: item.id,
            name: `${item.name}-${item.version}-${item.cpuArch}`,
          });
        });
      });
    },
    handleAdd() {
      this.display = true;
      this.type = "create";
      this.title = this.$t("add");
    },
    handleDelete() {
      this.display = true;
      this.type = "delete";
      this.title = this.$t("configure.delete_configure_group");
      let arr = [];
      let str = "";
      if (this.$refs.table.select_id == "2") {
        str = this.$refs.table.total.toString();
      } else {
        this.$refs.table.selectRow.rows.map(function (row) {
          arr.push(row.name);
        });
        str = arr.toString();
      }
      this.delete_content = this.$t(
        "configure.delete_configure_group_content"
      ).replace("%s", str);
    },
    async getAllIds() {
      let data = [];
      const res = await this.getConfigureGroup({
        ...this.$route.query,
        paged: false,
      });
      res.data.data.results.map((item) => {
        if (!this.$refs.table.selectRow.excludeIds.includes(item.id)) {
          data.push(item.id);
        }
      });
      return data;
    },
    handleImport() {
      this.display = true;
      this.type = "import";
      this.title = this.$t("import");
    },
    async handleExport() {
      let ids = [];
      if (this.$refs.table.select_id == "2") {
        ids = await this.getAllIds();
      } else {
        ids = this.$refs.table.selectRow.ids;
      }
      let res = await downloadConfigureGroup({ ids });
      download(res, "configure_group");
    },
    async handleDeleteOk() {
      let ids = [];
      if (this.$refs.table.select_id == "2") {
        ids = await this.getAllIds();
      } else {
        ids = this.$refs.table.selectRow.ids;
      }
      deleteConfigureGroup(ids).then((res) => {
        if (res.data.code === "0") {
          this.$message({
            type: "success",
            message: res.data.message,
          });
          this.$emit("click", "success");
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
        this.display = false;
        this.title = "";
        this.type = "";
        this.$refs.table.refresh();
      });
    },
    handleEdit(row) {
      this.display = true;
      this.row_id = row.id;
      this.type = "update";
      this.title = this.$t("edit");
    },
    handleIssue(row) {
      this.row_id = row.id;
      this.display = true;
      this.type = "issue";
      this.standarName = row.standardLibraryName;
      this.title = this.$t("configure.issue");
    },
    handleSuccess() {},
    handleError() {},
    handleClose(type, form) {
      if (type === "success") {
        this.display = false;
        this.type = "";
        this.id = "";
        this.batch_id = "";
        this.$refs.table.refresh();
      } else if (type === "system") {
        this.type = "system";
        this.title = this.$t("patch.analysis_system");
        this.id = form.id;
        this.batch_id = form.batchId;
      } else if (type === "select") {
        this.type = "issue";
        this.title = this.$t("configure.issue");
        this.id = "";
        this.batch_id = "";
      } else if (type === "report") {
        this.type = "report";
        this.title = this.$t("configure.different_report");
      } else {
        this.display = false;
        this.type = "";
        this.id = "";
        this.batch_id = "";
      }
    },
  },
};
</script>

<style>
</style>